<template>
	<view>
		<nav-bar fontColor="#FFFFFF" title="拣货">
			<view slot="right" @click="search">
				<text style="margin-right: 20rpx;font-weight: bold;">查询</text>
			</view>
		</nav-bar>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<view v-if="status">
			<view class="orderNumber">数量：{{total}}个</view>
			<mescroll-body @init="mescrollInit" @down="downCallback" @up="getDataList">

				<view v-for="(item,index) in dataList">
					<uni-list>
						<view style="padding: 20rpx;">
							<evan-checkbox-group @change="changeData" v-model="checkData">
								<evan-checkbox :key="item.id" :label="item.id"></evan-checkbox>
								<uni-list-item :index="index+1" :title="'物品名称:'+item.materialName + '\n数量:'+item.qty
								+ '\n入库批号:' +item.reciptLineNo" :note="'拣货员:'+item.pickUpUserName + '\n'+ '日期:'+item.requstDeliveryDate"
									:rightText="item.locationNo" link="reLaunch" clickable>
								</uni-list-item>
							</evan-checkbox-group>
						</view>
					</uni-list>
				</view>
			</mescroll-body>
			<view style="height: 60rpx;">
				<view class="bottomBox">
					<view class="buttonBox">
						<button class="button_fix" :disabled="checkData[0] == null" @click="nextStep"
							size="default">拣货</button>
					</view>
				</view>
			</view>

		</view>

		<!-- 上架弹窗 -->
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="repeatScan" title="入库批号" placeholder="请扫描入库批号:"
				@confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="1000"></uni-popup-message>
		</uni-popup>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :before-close="true" type="info" cancelText="回到首页" confirmText="继续绑卡" title="提示"
				content="还有其他物品需要继续绑卡吗？" @confirm="dialogConfirm()" @close="dialogClose()"></uni-popup-dialog>
		</uni-popup>
		<!-- 	<uni-popup ref="popup" @change="change">
			<view class="popup">
				<image src="../../static/img/status.png" mode=""></image>
				<view>请扫描RFID射频标签</view>
			</view>
		</uni-popup> -->

	</view>
</template>

<script>
	var main = null;
	var receiver = null;
	import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
	import uniList from '@/components/uni-list/uni-list.vue';
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			uniList,
		},
		data() {
			return {
				msgType: 'success',
				messageText: '这是一条成功提示',
				qrCode: '',
				number: '',
				orderNumber: "BC010302",
				//搜索内容
				downOption: {
					native: false,
					use: true, // 是否启用下拉刷新; 默认true
					auto: true
				},
				upOption: {
					use: true, // 是否启用上拉加载; 默认true
					auto: false,
					noMoreSize: 5, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
				},
				checkData: [],
				dataList: [],
				total: null,
				rfid: '',
				itemId: [], //存放每个案件id的数组
				status: true,
				rfidLength: 0,
				epc: ''
				// repeatRfid: [],//rfid重复数组
				// rfId: []//rfid数组无重复

			};
		},
		onLoad() {
			// this.startCheck();
			// this.getDataList();
			// this.get_gb();
			// uni.showLoading({
			// 	title: '加载中'
			// });
		},
		onBackPress() {
			if (receiver != null) {
				main.unregisterReceiver(receiver)
			}
		},
		onShow() {

		},
		//方法
		methods: {
			dialogInputConfirm(val) {
				// uni.showLoading({
				// 	mask: true,
				// 	title: '加载中...'
				// });
				//下面两行是手动输入的二维码和扫描的二维码合并在一起
				let scanDataList = val.scanData
				scanDataList.push(val.val)
				console.log('*****'+scanDataList)
				if(this.checkData.length != scanDataList.length){
					this.messageToggle('error','请重新找到库位并扫描')
					return;
				}
				
				//下面这一段意思是找出选择对应的locationNO
				let chooseDataCodeLIst = [];
				for(let i in this.dataList){
					for(let j in this.checkData){
						if(this.dataList[i].id == this.checkData[j]){
							chooseDataCodeLIst.push(this.dataList[i].locationNo)
						}
					}
				}
				console.log('########'+chooseDataCodeLIst)
				//对比
				for(let i in chooseDataCodeLIst){
					for(let j in scanDataList){
						if(chooseDataCodeLIst[i] == scanDataList[j]){
							console.log('找到')
						}
						else{
							this.messageToggle('error','请重新找到库位并扫描')
							return;
						}
					}
				}
				
				let opt = {
					url: 'web/tWmsDeliveryPickupPlan/pickUpAndOutbound',
					method: 'POST'
				};
				let params = {
					ids: this.checkData
					// qrOrRfids: scanDataList
				};
				this.$http.httpTokenRequest(opt, params).then(res => {
					if (res.data.code == 200) {
						this.messageToggle('success', res.data.msg)
						this.mescroll.resetUpScroll();
						uni.hideLoading();
					} else {
						this.messageToggle('error', res.data.msg)
						uni.hideLoading();
					}
				})
			},
			search() {
				uni.navigateTo({
					url: '/pages/order-pick/search'
				})
			},
			nextStep() {
				if (this.checkData[0] != null) {
					this.$refs.inputDialog.open();
				} else {
					this.messageToggle('error', '请选择内容')
				}
				// this.$refs.inputDialog.open();
			},
			changeData(r) {
				console.log(r)
			},
			goQrCode() {
				var that = this;
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						let code1 = res.result.slice(0, 2)
						let code2 = res.result.slice(2, 4)
						let code3 = res.result.slice(4, 6)
						let code4 = res.result.slice(6, 8)
						that.qrCode = code1 + '-' + code2 + '-' + code3 + '-' + code4
					}
				});
			},
			dialogConfirm() {
				this.$refs.alertDialog.close();
			},
			dialogClose() {
				this.$refs.alertDialog.close();
				uni.navigateBack({
					delta: 1
				});
			},
			// change(e) {
			// 	if (e.show == true) {
			// 		this.get_gb()
			// 	} else {
			// 		main.unregisterReceiver(receiver)
			// 	}
			// },
			getRfid() {
				var that = this;
				let opt = {
					url: 'api/generateRfidCode',
					method: 'POST'
				};
				that.$http.httpTokenRequest(opt, {}).then(res => {
					this.epc = 'AAAA' + res.data.rfidCode
					// this.rfid = 'AAAA' + res.data.rfidCode
					// this.rfidLength = 1;
					console.log(this.epc)
					that.goPrint();
				});

			},
			goPrint() {
				let opt = {
					url: 'oms/tbRelateItemTakeDetail/bindRfidAndPostPrint',
					method: 'POST'
				};
				let params = {
					ids: this.itemId,
					rfidCode: this.epc
				};
				this.$http.httpTokenRequest(opt, params).then(res => {

				})
			},
			messageToggle(type, msg) {
				this.msgType = type
				this.messageText = msg
				this.$refs.message.open();
			},
			get_gb() {
				var that = this;
				main = plus.android.runtimeMainActivity();
				receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						var data = intent.getExtra("data");
						if (data) {
							console.log(data)
							that.rfid = data
							that.rfidLength = 1;
							// that.repeatRfid.push(data)
							// for (let i = 0; i < that.repeatRfid.length; i++) {
							// 	if (that.rfId.indexOf(that.repeatRfid[i]) == -1) {
							// 		that.rfId.push(that.repeatRfid[i])
							// 		console.log(that.rfId)
							// 	}
							// }
						}
					}
				});
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				let filter = new IntentFilter();
				filter.addAction('android.rfid.epccode');
				var info = main.registerReceiver(receiver, filter)
				// main.unregisterReceiver(receiver)
			},
			goBindCard(e) {
				this.rfid = '';
				this.epc = '';
				this.itemId = [];
				this.itemId.push(e)
				console.log(this.itemId)
				// this.getRfid();
				this.status = !this.status
				// this.$refs.popup.open()
			},
			goBack() {
				this.rfid = ''
				this.rfidLength = 0;
				this.epc = '';
				this.status = !this.status;
			},
			getDataList(page) {
				let opt = {
					url: 'web/tWmsDeliveryPickupPlan/search/' + page.num + '/10',
					method: 'POST'
				};
				let params = {
					status: "created",
					// jobType:"recept",
				};
				this.$http.httpTokenRequest(opt, params).then(res => {
					if (res.data.code == 200) {
						// this.mescroll.endSuccess(res.data.data.records.length)
						this.mescroll.endByPage(res.data.data.records.length, res.data.data
							.total); //必传参数(当前页的数据个数, 总页数)
						// this.mescroll.endSuccess(res.data.data.records.length);
						this.total = res.data.data.total
						if (page.num == 1) {
							this.dataList = res.data.data.records;
						} else {
							this.dataList = this.dataList.concat(res.data.data.records);
						}
						if (res.data.data.records == '') {
							this.messageToggle('error', '暂无数据')
						}
						uni.hideLoading()
					} else {
						uni.hideLoading()
						this.messageToggle('error', res.data.msg)
					}

				}).catch(() => {
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			submit() {
				if (this.rfid == '') {
					this.messageToggle('error', '请扫描RFID射频标签')
					return
				} else {
					let opt = {
						url: 'oms/tbRelateItemTakeDetail/bindRfid',
						method: 'POST'
					};
					let params = {
						ids: this.itemId,
						rfidCode: this.rfid,
						jobType: "recept"
					};
					this.$http.httpTokenRequest(opt, params).then(res => {
						if (res.data.code == 200) {
							// this.$refs.popup.close()
							this.rfid = '';
							this.rfidLength = 0
							this.messageToggle('success', res.data.msg)
							this.getDataList()
							this.$refs.alertDialog.open();
							this.status = !this.status
						} else {
							this.messageToggle('error', res.data.msg)
							this.status = !this.status
							this.rfid = '';
							this.rfidLength = 0
							// this.$refs.popup.close()
						}
					})
				}

			},
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},

			/*下拉刷新的回调 */
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				//联网加载数据
				this.loadData(page.num);
			},
			// loadData(pageNo) {
			// 	let opts = {
			// 		url: 'mesg/yunheFireOrEventMesg/search/' + pageNo + '/' + 10,
			// 		method: 'POST'
			// 	};
			// 	let params = {
			// 		eventType: '林业巡检',
			// 		// contactId: uni.getStorageSync('userInfo').data.sysUser.id,
			// 		// status: 'COMPLETED'
			// 	};
			// 	this.$http.httpTokenRequest(opts, params).then(res => {
			// 			if (res.data.code == 200) {
			// 				this.mescroll.endSuccess(res.data.data.records.length)
			// 				if (pageNo == 1) {
			// 					this.dataList = res.data.data.records;
			// 				} else {
			// 					this.dataList = this.dataList.concat(res.data.data.records);
			// 				}
			// 				this.orderNumber = res.data.data.total
			// 				console.log(this.dataList)
			// 				uni.hideLoading();
			// 			} else if (res.data.code == 401) {
			// 				uni.hideLoading();
			// 				uni.showToast({
			// 					title: res.data.msg,
			// 					icon: 'none',
			// 					position: 'center'
			// 				});
			// 				setTimeout(() => {
			// 					uni.navigateTo({
			// 						url: './homePage'
			// 					})
			// 				}, 1500)
			// 			} else {
			// 				uni.hideLoading();
			// 				uni.showToast({
			// 					title: res.data.msg,
			// 					icon: 'none',
			// 					position: 'center'
			// 				});
			// 			}
			// 		})
			// 		.catch(res => {
			// 			this.mescroll.endErr();
			// 			uni.hideLoading();
			// 		});
			// }
		},
		//页面下来刷新
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 2000);
		}
	};
</script>
<style lang="scss" scoped>
	page {
		background: #FFFFFF;
	}

	.popup {
		width: 100%;

		view {
			font-size: 35rpx;
			color: #FFFFFF;
			margin-top: 50rpx;
			text-align: center;
			letter-spacing: 3rpx;
			font-weight: 700;
		}

		image {
			display: block;
			margin: 0 auto;
		}
	}

	.end-title {
		display: flex;
		border-bottom: 1px solid #E5E5E5;
	}

	.end-title view {
		flex-grow: 1;
		text-align: center;
		background: #FFFFFF;
		// border-top: 1px solid #E5E5E5;
	}

	.btna {

		border-bottom: 2px solid #007AFF;
		color: #007AFF;
	}

	.tab {
		padding: 30rpx;
		font-size: 35rpx;
	}

	.mapStyle {
		height: 1200rpx;
		width: 100%;
		margin-top: 10rpx;
	}

	/*说明*/
	.orderNumber {
		text-align: center;
		padding: 30rpx;
		font-size: 32rpx;
		letter-spacing: 3rpx;
		font-weight: 700;
		// font-family: '宋体';
		// font-weight: ;
	}

	.imgCode {
		position: relative;
		background: linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) left bottom,
			linear-gradient(#000000, #000000) left bottom;
		background-repeat: no-repeat;
		background-size: 2px 20px, 20px 2px;
		height: 400rpx;
		width: 400rpx;
		margin: 0 auto;
		margin-top: 35%;
	}

	.txtTop {
		margin-top: 50rpx;
	}

	.qrImg {
		width: 250rpx;
		height: 250rpx;
		left: 20%;
		top: 15%;
	}

	.qrTitle {
		font-size: 28rpx;
		color: #3B5DE0;
		margin-top: 90rpx;
		text-align: center;
		letter-spacing: 3rpx;
	}


	.bottomBox {
		position: fixed;
		width: 100%;
		bottom: 0;
		margin-bottom: 20rpx;
		z-index: 1000;
	}

	.buttonBox {
		width: 100%;
		margin: 0 auto;
		display: flex;
		justify-content: space-around;

		// margin-top: 200rpx;
	}

	.button_fix {
		width: 90%;
		height: 70rpx;
		// margin: 0 auto;
		color: #FFFFFF;
		background: #2749C8;
		font-weight: bold;
		letter-spacing: 5rpx;
		font-size: 28rpx;
		line-height: 70rpx;

	}
</style>